/*
 *   Copyright©2023 JoyCastle.
 *   All Rights Reserved.
 *   
 *   FileName:     AdjustWrapper.cs
 *   Author:       MSI-NB
 *   CreateTime:   2023/11/09 18:10:47
 *   UnityVersion: 2020.3.48f1c1
 *   Version:      1.0.0
 *   Description:
 *   
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using SevenPieceGame;
using com.adjust.sdk;
using UnityEngine;

namespace GTA
{
    public class AdjustWrapper : MonoSingleton<AdjustWrapper>
    {
        public void Init()
        {
            AdjustConfig adjustConfig = new AdjustConfig("qk20nkl18b9c", AdjustEnvironment.Production);
            adjustConfig.setLogLevel(AdjustLogLevel.Verbose);
            adjustConfig.setLogDelegate(msg => Debug.Log(msg));
            adjustConfig.setEventSuccessDelegate(EventSuccessCallback);
            adjustConfig.setEventFailureDelegate(EventFailureCallback);
            adjustConfig.setSessionSuccessDelegate(SessionSuccessCallback);
            adjustConfig.setSessionFailureDelegate(SessionFailureCallback);
            adjustConfig.setDeferredDeeplinkDelegate(DeferredDeeplinkCallback);
            adjustConfig.setAttributionChangedDelegate(AttributionChangedCallback);
            adjustConfig.needsCost = true;
            Adjust.start(adjustConfig);
        }

        public void LogEvent(string eventName, Dictionary<string, string> eventValues)
        {
            AdjustEvent adjustEvent = new AdjustEvent(eventName);
            if (eventValues != null)
            {
                foreach (var vk in eventValues)
                {
                    adjustEvent.addPartnerParameter(vk.Key, vk.Value);
                }
            }
            Adjust.trackEvent(adjustEvent);
        }
        /// <summary>
        /// 专门用于收入的事件
        /// </summary>
        /// <param name="AppEventName"></param>
        /// <param name="eventValues"></param>
        /// <param name="revenue">收入具体数</param>
        /// <param name="currency">币种</param>
        public void LogEventRevenue(string eventName, Dictionary<string, string> eventValues, double revenue,
            string currency)
        {
            AdjustEvent adjustEvent = new AdjustEvent(eventName);
            if (eventValues != null)
            {
                foreach (var vk in eventValues)
                {
                    adjustEvent.addPartnerParameter(vk.Key, vk.Value);
                }
            }
            adjustEvent.setRevenue(revenue, currency);
            Adjust.trackEvent(adjustEvent);
        }
        
        public void LogEventADRevenue(string eventName, double revenue)
        {
            AdjustEvent adjustEvent = new AdjustEvent(eventName);
            adjustEvent.setRevenue(revenue, "USD");
            adjustEvent.addPartnerParameter("price", revenue.ToString());
            Adjust.trackEvent(adjustEvent);
        }
        /*public void TestEvent()
        {
            AdjustEvent adjustEvent = new AdjustEvent("c7some");
            Adjust.trackEvent(adjustEvent);
            AdjustEvent adjustEvent2 = new AdjustEvent("x0ws27");
            Adjust.trackEvent(adjustEvent2);
            AdjustEvent adjustEvent3 = new AdjustEvent("ukzuj9");
            Adjust.trackEvent(adjustEvent3);
        }*/

        public void AttributionChangedCallback(AdjustAttribution attributionData)
        {
            GameLogger.LogError("Attribution changed!");

            if (attributionData.trackerName != null)
            {
                GameLogger.LogError("Tracker name: " + attributionData.trackerName);
            }
            if (attributionData.trackerToken != null)
            {
                GameLogger.LogError("Tracker token: " + attributionData.trackerToken);
            }
            if (attributionData.network != null)
            {
                GameLogger.LogError("Network: " + attributionData.network);
                //PublicDataMgr.Instance.SetMediaSource(attributionData.network);
                if (!attributionData.network.Equals("Organic"))
                {
                    UserLayerManager.Instance.ChangeUserLayer(UserLayerManager.UserLayerType.Install);
                }
                else
                {
                    UserLayerManager.Instance.ChangeUserLayer(UserLayerManager.UserLayerType.Organic);
                }
            }
            if (attributionData.campaign != null)
            {
                GameLogger.LogError("Campaign: " + attributionData.campaign);
                if (!attributionData.network.Equals("Organic"))
                {
                    UserLayerManager.Instance.ChangeUserLayer(UserLayerManager.UserLayerType.Install);
                }
                else
                {
                    UserLayerManager.Instance.ChangeUserLayer(UserLayerManager.UserLayerType.Organic);
                }
            }
            if (attributionData.adgroup != null)
            {
                GameLogger.LogError("Adgroup: " + attributionData.adgroup);
            }
            if (attributionData.creative != null)
            {
                GameLogger.LogError("Creative: " + attributionData.creative);
                //PublicDataMgr.Instance.SetAdset(attributionData.creative);
            }
            if (attributionData.clickLabel != null)
            {
                GameLogger.LogError("Click label: " + attributionData.clickLabel);
            }
            if (attributionData.adid != null)
            {
                GameLogger.LogError("ADID: " + attributionData.adid);
            }
//#if UNITY_ANDROID
            if (attributionData.fbInstallReferrer != null)
            {
                GameLogger.LogError("fbInstallReferrer: " + attributionData.fbInstallReferrer);
            }
//#endif
        }

        public void EventSuccessCallback(AdjustEventSuccess eventSuccessData)
        {
            GameLogger.LogError("Event tracked successfully!");

            if (eventSuccessData.Message != null)
            {
                GameLogger.LogError("Message: " + eventSuccessData.Message);
            }
            if (eventSuccessData.Timestamp != null)
            {
                GameLogger.LogError("Timestamp: " + eventSuccessData.Timestamp);
            }
            if (eventSuccessData.Adid != null)
            {
                GameLogger.LogError("Adid: " + eventSuccessData.Adid);
            }
            if (eventSuccessData.EventToken != null)
            {
                GameLogger.LogError("EventToken: " + eventSuccessData.EventToken);
            }
            if (eventSuccessData.CallbackId != null)
            {
                GameLogger.LogError("CallbackId: " + eventSuccessData.CallbackId);
            }
            if (eventSuccessData.JsonResponse != null)
            {
                GameLogger.LogError("JsonResponse: " + eventSuccessData.GetJsonResponse());
            }
        }

        public void EventFailureCallback(AdjustEventFailure eventFailureData)
        {
            GameLogger.LogError("Event tracking failed!");

            if (eventFailureData.Message != null)
            {
                GameLogger.LogError("Message: " + eventFailureData.Message);
            }
            if (eventFailureData.Timestamp != null)
            {
                GameLogger.LogError("Timestamp: " + eventFailureData.Timestamp);
            }
            if (eventFailureData.Adid != null)
            {
                GameLogger.LogError("Adid: " + eventFailureData.Adid);
            }
            if (eventFailureData.EventToken != null)
            {
                GameLogger.LogError("EventToken: " + eventFailureData.EventToken);
            }
            if (eventFailureData.CallbackId != null)
            {
                GameLogger.LogError("CallbackId: " + eventFailureData.CallbackId);
            }
            if (eventFailureData.JsonResponse != null)
            {
                GameLogger.LogError("JsonResponse: " + eventFailureData.GetJsonResponse());
            }

            GameLogger.LogError("WillRetry: " + eventFailureData.WillRetry.ToString());
        }

        public void SessionSuccessCallback(AdjustSessionSuccess sessionSuccessData)
        {
            GameLogger.LogError("Session tracked successfully!");

            if (sessionSuccessData.Message != null)
            {
                GameLogger.LogError("Message: " + sessionSuccessData.Message);
            }
            if (sessionSuccessData.Timestamp != null)
            {
                GameLogger.LogError("Timestamp: " + sessionSuccessData.Timestamp);
            }
            if (sessionSuccessData.Adid != null)
            {
                GameLogger.LogError("Adid: " + sessionSuccessData.Adid);
            }
            if (sessionSuccessData.JsonResponse != null)
            {
                GameLogger.LogError("JsonResponse: " + sessionSuccessData.GetJsonResponse());
            }
        }

        public void SessionFailureCallback(AdjustSessionFailure sessionFailureData)
        {
            GameLogger.LogError("Session tracking failed!");

            if (sessionFailureData.Message != null)
            {
                GameLogger.LogError("Message: " + sessionFailureData.Message);
            }
            if (sessionFailureData.Timestamp != null)
            {
                GameLogger.LogError("Timestamp: " + sessionFailureData.Timestamp);
            }
            if (sessionFailureData.Adid != null)
            {
                GameLogger.LogError("Adid: " + sessionFailureData.Adid);
            }
            if (sessionFailureData.JsonResponse != null)
            {
                GameLogger.LogError("JsonResponse: " + sessionFailureData.GetJsonResponse());
            }

            GameLogger.LogError("WillRetry: " + sessionFailureData.WillRetry.ToString());
        }

        private void DeferredDeeplinkCallback(string deeplinkURL)
        {
            GameLogger.LogError("Deferred deeplink reported!");

            if (deeplinkURL != null)
            {
                GameLogger.LogError("Deeplink URL: " + deeplinkURL);
            }
            else
            {
                GameLogger.LogError("Deeplink URL is null!");
            }
        }
    }
}

